package com.uduew.sjd.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Environment
import android.os.StatFs

object StorageInfoCollector {
    // 获取内存信息（包含运行内存）
    data class MemoryInfo(
        val totalRam: Long,
        val availableRam: Long
    )

    // 获取存储信息
    data class StorageInfo(
        val internalTotal: Long,
        val internalFree: Long,
        val externalTotal: Long,
        val externalUsed: Long,
        val externalPath: String
    )

    // 获取运行内存信息
    fun getMemoryInfo(context: Context): MemoryInfo {
        val memInfo = ActivityManager.MemoryInfo()
        (context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager)
            .getMemoryInfo(memInfo)
        return MemoryInfo(
            totalRam = memInfo.totalMem,
            availableRam = memInfo.availMem
        )
    }

    // 获取存储信息
    fun getStorageInfo(): StorageInfo {
        // 内部存储
        val internalStat = StatFs(Environment.getDataDirectory().path)
        val internalTotal = internalStat.totalBytes
        val internalFree = internalStat.availableBytes

        // 外部存储（SD卡）
        var externalTotal = 0L
        var externalUsed = 0L
        var externalPath = ""

        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
            val externalStat = StatFs(Environment.getExternalStorageDirectory().path)
            externalTotal = externalStat.totalBytes
            externalUsed = externalStat.totalBytes - externalStat.availableBytes
            externalPath = Environment.getExternalStorageDirectory().absolutePath
        }

        return StorageInfo(
            internalTotal = internalTotal,
            internalFree = internalFree,
            externalTotal = externalTotal,
            externalUsed = externalUsed,
            externalPath = externalPath
        )
    }
}
